Private Sub cmdList_Click()

Dim i As Long, l As Long, j As Long, x As Long
Dim ExportHandle As Long, counter As Long
Dim strCellName As String, strFilename As String
Dim CellWritten As Boolean

If Val(lblCount(7).Caption) <= 0 Then
    MsgBox "No cells found in current file."
    Exit Sub
ElseIf Val(lblCount(28).Caption) <= 0 Then
    MsgBox "No NPCs found in current file."
    Exit Sub
End If

cdlOpen.Flags = cdlOFNOverwritePrompt

cdlOpen.FileName = ""
cdlOpen.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)| *.*"
cdlOpen.ShowOpen
If cdlOpen.FileName = "" Then Exit Sub
cdlOpen.InitDir = Left$(cdlOpen.FileName, Len(cdlOpen.FileName) - (Len(cdlOpen.FileTitle) + 1))
On Error Resume Next
'If Dir$(cdlOpen.FileName) <> "" Then Kill (cdlOpen.FileName)

strCellName = InputBox("Enter any part of cell name:" & vbCrLf & vbCrLf & "NOTE: Case sensitive", "ESParser", "")
If strCellName = "" Then Exit Sub

ExportHandle = FreeFile

lblLog = lblLog & "-Generating NPC List from """ & Dir$(CurrentlyLoadedFile) & """..." & vbCrLf
lblLog.Refresh
Open cdlOpen.FileName For Binary As ExportHandle

'Code description, etc:
Put ExportHandle, , "[list]"
Put ExportHandle, , vbCrLf
Put ExportHandle, , vbCrLf

'Actual NPCs:
For i = LBound(CELL) To UBound(CELL)
    For j = LBound(CELL(i).References) To UBound(CELL(i).References)
        For l = LBound(NPC_) To UBound(NPC_)
            If NPC_(l).NAMEValue = CELL(i).References(j).NAMEValue Then
                If CellWritten = 0 Then
                    Put ExportHandle, , CELL(i).NAMEValue
                    If CELL(i).RGNNSize <> 0 Then
                        Put ExportHandle, , " ("
                        Put ExportHandle, , CStr(CELL(i).DATAGridX)
                        Put ExportHandle, , ", "
                        Put ExportHandle, , CStr(CELL(i).DATAGridY)
                        Put ExportHandle, , ")"
                    End If
                    Put ExportHandle, , ":"
                    Put ExportHandle, , vbCrLf
                    Put ExportHandle, , vbCrLf
                    CellWritten = 1
                End If
                If InStr(1, NPC_(l).FNAMValue, "guard", vbTextCompare) Or InStr(1, NPC_(l).FNAMValue, "slave", vbTextCompare) Or InStr(1, NPC_(l).FNAMValue, "Cattle", vbTextCompare) Or InStr(1, NPC_(l).FNAMValue, "sleeper", vbTextCompare) <= 0 Then
                    If NPC_(l).NPDTSize = 52 Then
                        Put ExportHandle, , NPC_(l).FNAMValue & ": " & NPC_(l).RNAMValue & " " & NPC_(l).CNAMVALUE & " level " & NPC_(l).NPDTDataL.NPDTLevel
                    Else
                        Put ExportHandle, , NPC_(l).FNAMValue & ": " & NPC_(l).RNAMValue & " " & NPC_(l).CNAMVALUE & " level " & NPC_(l).NPDTDataS.NPDTLevel
                    End If
                End If
                Put ExportHandle, , vbCrLf
                counter = counter + 1
            End If
        Next
    Next
    Put ExportHandle, , vbCrLf
    Put ExportHandle, , vbCrLf
    Put ExportHandle, , vbCrLf
    Put ExportHandle, , vbCrLf
    CellWritten = 0
Next

Put ExportHandle, , "[/list]"

Close ExportHandle
If counter <= 0 Then
    lblLog = lblLog & "-No NPCs found in given cell in """ & Dir(cdlOpen.FileName) & """" & vbCrLf
Else
    lblLog = lblLog & "-Listed " & counter & " NPCs from """ & Dir(cdlOpen.FileName) & """ successfully." & vbCrLf
End If

Exit Sub

ErrHandler:
    MsgBox "ESParser has encountered an error number #" & Err.Number & ": " & Err.Description & "."
    lblLog = lblLog & "-An error occured during NPC listing." & vbCrLf

End Sub